package com.ruoyi.web.controller.wx;


import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.vo.WxParamVo;
import com.ruoyi.framework.config.WxMpNewServiceConfig;
import com.ruoyi.system.domain.DyClient;
import com.ruoyi.system.service.IDyClientService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/wx/token")
@Slf4j
public class WxTokenController extends BaseController {

    @Resource
    private WxMpService wxMpService;
    @Resource
    private IDyClientService iDyClientService;
    @Resource
    private WxMpUserService wxMpUserService;
    @Resource
    private WxMpNewServiceConfig wxMpNewServiceConfig;



    @GetMapping("/checkToken")
    @SaIgnore
    public String checkToken(
            @RequestParam(name = "signature", required = false) String signature,
            @RequestParam(name = "timestamp", required = false) String timestamp,
            @RequestParam(name = "nonce", required = false) String nonce,
            @RequestParam(name = "echostr", required = false) String echostr
    ) {
        if (StrUtil.isAllBlank(signature, timestamp, nonce, echostr)) {
            throw new IllegalArgumentException("请求参数非法，请核实!");
        }
        if (wxMpService.checkSignature(timestamp, nonce, signature)){
            return echostr;
        }
        return "请求参数非法";
    }

    @GetMapping("/checkNewToken")
    @SaIgnore
    public String checkNewToken(
            @RequestParam(name = "signature", required = false) String signature,
            @RequestParam(name = "timestamp", required = false) String timestamp,
            @RequestParam(name = "nonce", required = false) String nonce,
            @RequestParam(name = "echostr", required = false) String echostr
    ) {
        if (StrUtil.isAllBlank(signature, timestamp, nonce, echostr)) {
            throw new IllegalArgumentException("请求参数非法，请核实!");
        }
        if (wxMpNewServiceConfig.wxNewMpService().checkSignature(timestamp, nonce, signature)){
            return echostr;
        }
        return "请求参数非法";
    }

    /**
     * 获取openId
     */
    @PostMapping("/getOpenId")
    @SaIgnore
    public R<WxParamVo> getOpenId(@RequestBody WxParamVo wxParamVo) {
        try {
            WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(wxParamVo.getCode());
            wxParamVo.setOpenId(accessToken.getOpenId());
            wxParamVo.setAccessToken(accessToken.getAccessToken());

            DyClient client = iDyClientService.createClient(accessToken.getOpenId());

        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new ServiceException("获取accessToken发生错误!");
        }
        return R.ok(wxParamVo);
    }

    /**
     * 拉取用户信息
     * openId
     */
    @PostMapping("/getUserInfo")
    @SaIgnore
    public R<DyClient> userInfo(@RequestBody WxParamVo paramVo) {
//        try {
//            mpUser = wxMpUserService.userInfo(paramVo.getOpenId());
//        } catch (WxErrorException e) {
//            e.printStackTrace();
//        }

        DyClient client = iDyClientService.getUserInfoByOpenId(paramVo.getOpenId());
        return R.ok(client);
    }

    /**
     * 获取初始化信息
     */
    @PostMapping("/getSignature")
    @SaIgnore
    public R<WxJsapiSignature> getSignature(@RequestBody WxParamVo paramVo) {
        WxJsapiSignature signature = null;
        try {
            signature = wxMpService.createJsapiSignature(paramVo.getUrl());
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return R.ok(signature);
    }


}
